package xmpp.androidpush.aidl;

import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Message.Type;

public class PushMessage implements Parcelable {
    private String from;
    private String to;
    private String msg;
    private HashMap<String, String> propertys;
    private HashMap<String, String> extensions;


    private ArrayList<PacketExtension> packetExtension ;

    public PushMessage() {
    }

    public PushMessage(String from, String to, String msg) {
        this.from = from;
        this.to = to;
        this.msg = msg;
    }
    public PushMessage(String from, String to, String msg, ArrayList<PacketExtension> packetExtensionList) {
        this.from = from;
        this.to = to;
        this.msg = msg;
        this.packetExtension = packetExtensionList;
    }
    public PushMessage(Packet msg) {
        this.from = msg.getFrom();
        this.to = msg.getTo();
        if (msg instanceof Message) {
            this.msg = ((Message) msg).getBody();
        } else if (msg instanceof Presence) {
            this.msg = ((Presence) msg).getStatus();
        }

        Iterator var3 = msg.getPropertyNames().iterator();
        while (var3.hasNext()) {
            String name = (String) var3.next();
            this.getPropertys().put(name, msg.getProperty(name).toString());
        }
//        Iterator iterator = msg.getExtensions().iterator();
//        while (iterator.hasNext()) {
//            DefaultPacketExtension packetExtension = (DefaultPacketExtension) iterator.next();
//            Iterator names = packetExtension.getNames().iterator();
//            while (names.hasNext()) {
//                String name = (String) names.next();
//                String value = packetExtension.getValue(name);
//                this.getExtensions().put(name, value);
//            }
//        }

        Iterator<PacketExtension> iterator =  msg.getExtensions().iterator();
        while (iterator.hasNext()){
            PacketExtension extension =  iterator.next();
            this.getPacketExtension().add(extension);
        }
    }

    public String getFrom() {
        return this.from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return this.to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Collection<String> getPropertyNames() {
        return this.getPropertys().keySet();
    }
    public Collection<String> getExtensionsNames() {
        return this.getExtensions().keySet();
    }
    public String getProperty(String name) {
        return (String) this.getPropertys().get(name);
    }

    private HashMap<String, String> getPropertys() {
        if (this.propertys == null) {
            this.propertys = new HashMap();
        }

        return this.propertys;
    }
    public ArrayList<PacketExtension> getPacketExtension() {
        if (this.packetExtension == null) {
            this.packetExtension = new ArrayList<>();
        }

        return this.packetExtension;
    }
    public void setProperty(String name, String value) {
        this.getPropertys().put(name, value);
    }

    public HashMap<String, String> getExtensions() {
        if (this.extensions == null) {
            this.extensions = new HashMap();
        }
        return this.extensions;
    }

    public void setExtension(String name, String value) {
        this.getExtensions().put(name, value);
    }

    public Message toMessage() {
        Message msg = new Message();
        msg.setFrom(this.from);
        msg.setTo(this.to);
        msg.setType(Type.chat);
        msg.setBody(this.msg);
        Iterator var3 = this.getPropertyNames().iterator();
        while (var3.hasNext()) {
            String name = (String) var3.next();
            msg.setProperty(name, this.getProperty(name));
        }
        msg.addExtensions(this.getPacketExtension());
        return msg;
    }

    public String toString() {
        return "PushMessage [from=" + this.from + ", to=" + this.to + ", msg=" + this.msg + ", propertys=" + this.propertys + "]";
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.from);
        dest.writeString(this.to);
        dest.writeString(this.msg);
        dest.writeSerializable(this.propertys);
        dest.writeSerializable(this.extensions);
        dest.writeList(this.packetExtension);
    }

    protected PushMessage(Parcel in) {
        this.from = in.readString();
        this.to = in.readString();
        this.msg = in.readString();
        this.propertys = (HashMap<String, String>) in.readSerializable();
        this.extensions = (HashMap<String, String>) in.readSerializable();
        this.packetExtension = new ArrayList<PacketExtension>();
        in.readList(this.packetExtension, PacketExtension.class.getClassLoader());
    }

    public static final Parcelable.Creator<PushMessage> CREATOR = new Parcelable.Creator<PushMessage>() {
        @Override
        public PushMessage createFromParcel(Parcel source) {
            return new PushMessage(source);
        }

        @Override
        public PushMessage[] newArray(int size) {
            return new PushMessage[size];
        }
    };
}
